package com.minjiang.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.minjiang.common.result.Result;
import com.minjiang.model.vo.MyUserQueryVo;
import com.minjiang.model.system.MyUser;
import com.minjiang.system.service.sec.MyUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/system/myUser")
public class MyUserController {
    @Autowired
    private MyUserService myUserService;

    @ApiOperation(value = "获取用户分页列表")
    @GetMapping("/{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "userQueryVo", value = "查询对象", required = false)
                    MyUserQueryVo userQueryVo) {
        Page<MyUser> pageParam = new Page<>(page, limit);
        IPage<MyUser> pageModel = myUserService.selectPage(pageParam, userQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "根据id获取用户")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable String id) {
        MyUser user = myUserService.getById(id);
        return Result.ok(user);
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable String id){
        boolean isOk = myUserService.removeById(id);
        if (isOk){
            return Result.ok();
        }
        return Result.fail("删除用户失败");
    }

    @ApiOperation(value = "添加用户")
    @PostMapping("/save")
    public Result save(@RequestBody MyUser myUser){
        boolean save = myUserService.save(myUser);
        if (save){
            return Result.ok();
        }
        return Result.fail("添加用户失败");
    }

    @ApiOperation(value = "更新用户")
    @PutMapping("/update")
    public Result updateById(@RequestBody MyUser myUser) {
        myUserService.updateById(myUser);
        return Result.ok();
    }
}
